
package fileby.util.properties;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.jdesktop.application.ResourceConverter.ResourceConverterException;

/**
 * Non mutable class which gives resources to a certain class.
 * @author Ludwig Kjellstrom
 */
public class PropertyBundle {

    private PropertyResourceBundle p;

    public PropertyBundle(Class clazz) throws IOException {
        p = new PropertyResourceBundle(clazz.
                getResourceAsStream(getPathOfProperties(clazz)));
    }

    public PropertyBundle() throws IOException {
        InputStream resourceStream = this.getClass().
                getResourceAsStream("resources/Global.properties");
        p = new PropertyResourceBundle(resourceStream);
    }

    public String getString(String key){
        String s = null;
        try{
        s = p.getString(key);
        }
        catch(java.util.MissingResourceException e){
            sendErrMessage(key,e);
        }
        return s;
    }

    public Object getObject(String key) {
        Object o = null;
        try {
            o = p.getObject(key);
        } catch (java.util.MissingResourceException e) {
            sendErrMessage(key, e);
        }
        return o;
    }

    public Font getFont(String key) {
        Font font = null;
        try {
            font = Font.decode(p.getString(key));
        } catch (java.util.MissingResourceException e) {
            sendErrMessage(key, e);
        }
        return font;
    }

    public Icon getIcon(String key){
        Icon icon = null;
        try {

            java.net.URL imageURL   = this.getClass().
                    getClassLoader().getResource(p.getString(key));
            icon = new ImageIcon(imageURL);
        }
        catch ( java.util.MissingResourceException e){
            sendErrMessage(key,e);
        }
        return icon;
    }

    public Color getColor(String key){
        Color color = null;
        try {
            color = (Color)parseString(p.getString(key));
        }
        catch(java.util.MissingResourceException e){
            sendErrMessage(key,e);
        } catch(ResourceConverterException e){
            sendErrMessage(key,e);
        }
        return color;
    }

    private String getPathOfProperties(Class clazz){
        String dir = "resources/" + clazz.getSimpleName() + ".properties";
        return dir;
    }

    private void sendErrMessage(String key, Exception e){
        System.err.println("Resource \"" + key + "\" could not be found. \n \t" +
                e.getMessage());
    }

    /*
     * This code is borrowed from the Swing Application
     * Framework made by Sun Microsystems.
     */
    private Object parseString(String s)throws ResourceConverterException {
        Color color = null;
        if (s.startsWith("#")) {
            switch (s.length()) {
                // RGB/hex color
                case 7:
                    color = Color.decode(s);
                    break;
                // ARGB/hex color
                case 9:
                    int alpha = Integer.decode(s.substring(0, 3));
                    int rgb = Integer.decode("#" + s.substring(3));
                    color = new Color(alpha << 24 | rgb, true);
                    break;
                default:
                    throw new ResourceConverterException("invalid #RRGGBB or #AARRGGBB color string", s);
            }
        } else {
            String[] parts = s.split(",");
            if (parts.length < 3 || parts.length > 4) {
                throw new ResourceConverterException("invalid R, G, B[, A] color string", s);
            }
            try {
                // with alpha component
                if (parts.length == 4) {
                    int r = Integer.parseInt(parts[0].trim());
                    int g = Integer.parseInt(parts[1].trim());
                    int b = Integer.parseInt(parts[2].trim());
                    int a = Integer.parseInt(parts[3].trim());
                    color = new Color(r, g, b, a);
                } else {
                    int r = Integer.parseInt(parts[0].trim());
                    int g = Integer.parseInt(parts[1].trim());
                    int b = Integer.parseInt(parts[2].trim());
                    color = new Color(r, g, b);
                }
            } catch (NumberFormatException e) {
                throw new ResourceConverterException("invalid R, G, B[, A] color string", s, e);
            }
        }
        return color;
    }

}
